package com.feicheng.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.base.batch.Outbatch;
import com.feicheng.base.common.DaoConstant;
import com.feicheng.base.exception.SystemException;
import com.feicheng.base.result.Result;
import com.feicheng.entity.OutboundHeader;
import com.feicheng.entity.OutboundItem;
import com.feicheng.model.OutboundHeaderWithItem;
import com.feicheng.model.OutboundWithSale;
import com.feicheng.model.OutboundWithSaleWithWarehouse;
import com.feicheng.model.ProductWithNum;
import com.feicheng.service.IOutboundHeaderService;
import com.feicheng.service.IOutboundItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao.zhang
 * @since 2022-07-04
 */
@RestController("/outbound")
@RequestMapping("/outbound")
public class OutboundHeaderController {

    @Autowired
    IOutboundHeaderService iOutboundHeaderService;

    @Autowired
    IOutboundItemService iOutboundItemService;

    /**
     * 出库产品分页查询
     * @param outboundHeaderId 出库表主键ID
     * @param current 当前页
     * @param size 每页条数
     * @author jing.zhou
     * @return
     */
    @PostMapping("/detail/page/g")
    public Result outDetail(
             Integer outboundHeaderId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
            ) {

        // 当出库表主键ID没有传的时候
        if (null == outboundHeaderId) {
        // 抛出异常
            throw new SystemException("outboundHeaderId不能为空");
        }

        // 构造条件查询
        QueryWrapper<OutboundWithSale> outDetailQueryWrapper = new QueryWrapper<>();

        // 把编号当作条件，放在构造器当作查询
        outDetailQueryWrapper.eq(DaoConstant.OUTBOUNDHEADER.OUTBOUND_HEADER_ID,outboundHeaderId);

        // 根据条件查询数据
        OutboundWithSale outboundWithSale = iOutboundHeaderService.selectOutbound(outDetailQueryWrapper);

        // 创建结果集
        JSONObject result = new JSONObject();

        // 把查询到的数据装到结果集中
        result.put(DaoConstant.MODEL.OUTBOUNDWITHSALE.MODEL_NAME, outboundWithSale);

        // 构建分页条件
        Page<ProductWithNum> page = new Page<>(current,size);

        // 构建条件查询
        QueryWrapper<ProductWithNum> productWithNumQueryWrapper = new QueryWrapper<>();

        // 把编号当作条件，放在构造器当作查询
        productWithNumQueryWrapper.eq(DaoConstant.OUTBOUNDITEM.OUTBOUND_HEADER_ID,outboundHeaderId);

        // 根据条件查询数据
        Page<ProductWithNum> productWithNumPage = iOutboundItemService.selectProduct(page, productWithNumQueryWrapper);

        // 把查询到的数据装到结果集中
        result.put(DaoConstant.MODEL.PRODUCTWITHNUM.MODEL_NAME,productWithNumPage);

        //返回结果集
        return Result.success(result);
    }

    /**
     * 出库列表条件分页查询
     * @param current 当前页
     * @param size 当前条数
     * @param OutboundWithSaleWithWarehouse 出库信息
     * @author wei.zhang
     * @return
     */
    @PostMapping("/page/g")
    public Result getOutbound(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody OutboundWithSaleWithWarehouse OutboundWithSaleWithWarehouse) {
        //构建查询条件
        QueryWrapper<OutboundWithSaleWithWarehouse> outboundQueryWrapper = new QueryWrapper<>();

        //查询仓库名称不为空
        if (StringUtils.hasLength(OutboundWithSaleWithWarehouse.getWarehouseName())) {
          outboundQueryWrapper.eq(DaoConstant.MODEL.OUTBOUNDWITHSALEWITHWAREHOUSE.WAREHOUSE_NAME, OutboundWithSaleWithWarehouse.getWarehouseName());
        }

        //查询出库批次号不为空
        if (StringUtils.hasLength(OutboundWithSaleWithWarehouse.getBatchNo())) {
            outboundQueryWrapper.eq(DaoConstant.OUTBOUNDHEADER.BATCH_NO, OutboundWithSaleWithWarehouse.getBatchNo());
        }

        //查询管理员不为空
        if (StringUtils.hasLength(OutboundWithSaleWithWarehouse.getUserName())) {
            outboundQueryWrapper.eq(DaoConstant.MODEL.OUTBOUNDWITHSALEWITHWAREHOUSE.USER_NAME, OutboundWithSaleWithWarehouse.getUserName());
        }

        //查询销售批次号不为空
        if (StringUtils.hasLength(OutboundWithSaleWithWarehouse.getSaleNo())) {
            outboundQueryWrapper.eq(DaoConstant.MODEL.OUTBOUNDWITHSALEWITHWAREHOUSE.SALE_NO, OutboundWithSaleWithWarehouse.getSaleNo());
        }

        //查询开始时间
        if (null != OutboundWithSaleWithWarehouse.getStartDate()) {
            outboundQueryWrapper.ge(DaoConstant.OUTBOUNDHEADER.DATE, OutboundWithSaleWithWarehouse.getStartDate());
        }

        //查询结束时间
        if (null != OutboundWithSaleWithWarehouse.getEndDate()) {
            outboundQueryWrapper.le(DaoConstant.OUTBOUNDHEADER.DATE, OutboundWithSaleWithWarehouse.getEndDate());
        }

        //构建分页
        Page<OutboundWithSaleWithWarehouse> page = new Page<>(current, size);

        return Result.success(iOutboundHeaderService.getOutbound(page, outboundQueryWrapper));


    }

    /**
     *新增出库借口
     * @param outboundHeaderWithItem  出库信息
     * @author siyu.liu
     * @since 2022-07-06
     */
    @PostMapping("/info")
    public Result insertOutbound(
            @RequestBody OutboundHeaderWithItem outboundHeaderWithItem){

        //非空验证
        if (null == outboundHeaderWithItem.getSaleId()){
            throw new SystemException("对应销售订单ID不能为空!");
        }
        if (null == outboundHeaderWithItem.getWarehouseId()){
            throw new SystemException("对应仓库ID不能为空!");
        }
        if (null == outboundHeaderWithItem.getUserId()){
            throw new SystemException("对应员工信息ID不能为空!");
        }

        //for循环判断入库体是否为空
        for (int i = 0; i < outboundHeaderWithItem.getOutboundItemList().size(); i++) {
            if (null == outboundHeaderWithItem.getOutboundItemList().get(i).getProductId()){
                throw new SystemException("产品型号ID不能为空!");
            }
            if (null == outboundHeaderWithItem.getOutboundItemList().get(i).getOutboundProductNum()){
                throw new SystemException("出库商品数量不能为空!");
            }
        }

        //创建获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();

        OutboundHeader outboundHeader = new OutboundHeader();

        //装配出库时间
        outboundHeader.setDate(localDateTime);

        //装配产品批次号
        outboundHeader.setBatchNo(Outbatch.getBatch(outboundHeaderWithItem.getUserId(),
                outboundHeaderWithItem.getWarehouseId()));

        //获取对应销售订单ID
        outboundHeader.setSaleId(outboundHeaderWithItem.getSaleId());

        //获取对应仓库ID
        outboundHeader.setWarehouseId(outboundHeaderWithItem.getWarehouseId());

        //获取员工信息ID
        outboundHeader.setUserId(outboundHeaderWithItem.getUserId());



        //将信息插入数据库
        boolean save = iOutboundHeaderService.save(outboundHeader);

        if (save){
            /*
             * 出库头添加成功后添加入库体*/

            //创建出库体订单单体List集合
            List<OutboundItem> outboundItems = outboundHeaderWithItem.getOutboundItemList();

            //获取list集合，将出库订单头编号插入
            for (OutboundItem outboundItem : outboundItems){
                outboundItem.setOutboundHeaderId(outboundHeaderWithItem.getOutboundHeaderId());
            }
        }

        //保存信息，执行返回结果
        boolean bacth = iOutboundItemService.saveBatch(outboundHeaderWithItem.getOutboundItemList());

        if (bacth){
            return Result.success("添加成功");
        }else {
            return Result.fail("添加失败",outboundHeaderWithItem);
        }

    }

}


